/*
 * 文件名：PushParkLot.java	 
 * 时     间：下午7:11:21
 * 作     者：Administrator       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.manager.thread;

import com.cz073586.tools.util.ThreadPoolUtil;
import com.local.common.MessageContains;
import com.local.entity.vo.CarParkInOverVo;
import com.local.entity.vo.ScreenMessage;
import com.local.manager.cfgManager.CloudConfigManager;
import com.local.manager.locks.RecordLockManager;
import com.local.model.LocalChargeBytimeType;
import com.local.model.Screen;
import com.local.service.face.CarInAndOutService;
import com.qianhai.common.utils.Log4jUtil;
import com.qianhai.common.utils.Tools;

/**
 * 
 * 类描述：本地进场数据处理
 * 
 * @ClassName: CarParkInLocalThread
 * @author lt
 * @date 2016年3月25日 下午5:37:36
 */
public class CarParkInLocalThread implements Runnable
{
	private static Log4jUtil log = Log4jUtil
			.getLog4j(CarParkInLocalThread.class);
	
	private CarParkInOverVo carInVo;
	
	private CarInAndOutService carInAndOutService;
	
	public CarParkInLocalThread(CarParkInOverVo carInVo,
			CarInAndOutService carInAndOutService)
	{
		super();
		this.carInVo = carInVo;
		this.carInAndOutService = carInAndOutService;
	}
	
	@Override
	public void run()
	{
		try
		{
			// 添加自动、手动开闸状态 2016-4-30 1.0 自动开闸
			if (carInVo.isOpen())
			{
				carInVo.getCarInPushVo().setIsOpenGate("1");
				carInVo.getCarparkIn().setVersion(1.0);
			} else
			{
				carInVo.getCarparkIn().setStatus(2);
			}
			String carNoType = carInVo.getCarNoType();
			LocalChargeBytimeType ltc = carInAndOutService
					.makeCarNoType(carInVo.getCarparkIn());
			carInVo.getCarInPushVo().setCarNoTye(
					!Tools.isEmpty(ltc) && !Tools.isEmpty(ltc.getName()) ? ltc
							.getName() : MessageContains.TEMP_CAR_TYPE);
			carInVo.getCarparkIn().setBrand(
					!Tools.isEmpty(ltc) && !Tools.isEmpty(ltc.getType()) ? ltc
							.getType() : "102");
			if (carNoType.equalsIgnoreCase("会员"))
			{
				carInVo.getCarparkIn().setBrand("100");
			}
			if (carNoType.equalsIgnoreCase("免费车"))
			{
				carInVo.getCarparkIn().setBrand("101");
			}
			if (carNoType.equalsIgnoreCase("临时车")
					&& !Tools.isEmpty(carInVo.getCarparkIn().getBrand()))
			{
				carInVo.getCarInPushVo().setCarNoTye(
						!Tools.isEmpty(ltc) ? ltc.getName() : carNoType);
				carInVo.getCarparkIn().setBrand(
						!Tools.isEmpty(ltc) ? ltc.getType() : "102");
			}
			// 插入进场纪录，和出场仓库（出场时结果）
			carInAndOutService.localCarParkInData(carInVo);
			
			if (!Tools.isEmpty(carInVo.getCarparkIn()))
			{
				if (log.isDebugEnabled())
				{
					log.debug("->进场纪录解锁,处理进场纪录子线程ID"
							+ Thread.currentThread().getId() + "，纪录ID"
							+ carInVo.getCarparkIn().getRecordId() + ",车牌号:"
							+ carInVo.getCarparkIn().getCarNo());
				}
				// 解锁
				RecordLockManager.instance().removeLock(
						carInVo.getCarparkIn().getRecordId());
			}
			
			// 判断是否重复入场
			if (!carInVo.isReptCarNoWhenCarInCrt() && carInVo.isReptCaoNo()) // 开关打开,并且已重复
			{
				
				// carInVo.getCarInPushVo().setPageShow("重复进场");;
				carInVo.getCarInPushVo().setReptCarIn("重复进场");
				carInVo.getCarInPushVo().setIsOpenGate("2"); // 不开闸弹框
			}
			
			// 推送到前端页面是否开闸状态，包括车型、免费卡、是否闸机是否开启
			if (!Tools.isEmpty(carInVo.getCarInPushVo()))
			{
				carInAndOutService.findPushCarInBillInfo(carInVo
						.getCarInPushVo());
			}
			
			// 推送总车位数量
			if (carInVo.isOpen())
			{
				carInAndOutService.pushParkLot();
			}
			
			// 如果开闸上传数据到云
			if (carInVo.isOpen() && CloudConfigManager.instance().isOpen()) // 开闸标记、计费副本列表、云配置
			{
				// 推送进场记录到云
				carInAndOutService.pushCarParkInInfo(carInVo);
			}// end upload to cloud
			
		} catch (Throwable e)
		{
			if (!Tools.isEmpty(carInVo.getCarparkIn()))
			{
				if (log.isDebugEnabled())
				{
					log.debug("->进场纪录异常解锁,线程ID"
							+ Thread.currentThread().getId() + "纪录ID"
							+ carInVo.getCarparkIn().getRecordId() + ",车牌号:"
							+ carInVo.getCarparkIn().getCarNo());
				}
				// 解锁
				RecordLockManager.instance().removeLock(
						carInVo.getCarparkIn().getRecordId());
			}
			
			log.error("处理进场报错自动开闸报错", e);
		} finally
		{
			// 检查控制汇总开关 2016-71-24
			String carmerNo = carInVo.getCarparkIn().getEquipment();
			if(carInVo.isCollect())
			{
				carmerNo = carInVo.getCollectCtrEqNo();
			}
			// 检查控制汇总开关 2016-71-24
			// 发送屏幕和信号灯
			Screen screen = carInAndOutService.queryScreenByCamelDeviceId(carmerNo);
			carInVo.setScreen(screen);
			ScreenMessage screenMessage = new ScreenMessage();
			if (carInVo.isOpen())
			{
				screenMessage
						.setHeadMsg(Tools.isEmpty(carInVo.getCarparkIn()) ? ""
								: carInVo.getCarparkIn().getCarNo());// 第一层，显示车牌
				screenMessage.setThirdMsg(MessageContains.WELCOM_MSG);// 第二层显示欢迎光临
				carInVo.setScreenMsg(screenMessage);
				if (!Tools.isEmpty(carInVo.getScreenMsg()))
				{
					ParkTwoScreenMsgThread parkScreenMsgThread = new ParkTwoScreenMsgThread(
							carInVo.getScreenMsg(), carInVo.getScreen());
					ThreadPoolUtil.instance().execute(parkScreenMsgThread);
				}
			} else if (!"无效车牌".equals(carInVo.getCarparkIn().getCarNo()))
			{
				screenMessage.setHeadMsg(carInVo.getCarparkIn().getCarNo());
				screenMessage.setThirdMsg("");
				carInVo.setScreenMsg(screenMessage);
				if (!Tools.isEmpty(carInVo.getScreenMsg()))
				{
					ParkTwoScreenMsgThread parkScreenMsgThread = new ParkTwoScreenMsgThread(
							carInVo.getScreenMsg(), carInVo.getScreen());
					ThreadPoolUtil.instance().execute(parkScreenMsgThread);
				}
			}
		}
	}
}
